package com.ymatou.iapi.settlement.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.settlement.parameter.GetAdSettlementCostReq;
import com.ymatou.iapi.settlement.service.QueryAdSettlementCostCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

/**
 * 根据时间范围查询广告结算费用信息
 */
@TestSuite
public class Ts_QueryAdSettlementCost {
	private static GetAdSettlementCostReq queryAdSettlementInfoReq;
	private static QueryAdSettlementCostCall queryAdSettlementInfoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据时间范围查询广告结算费用信息");
	}

	@Before
	public void caseUp() {
		queryAdSettlementInfoReq = new GetAdSettlementCostReq();
		queryAdSettlementInfoCall = new QueryAdSettlementCostCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryAdSettlementInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@SuppressWarnings("rawtypes")
	private void verifyDb() {

		BigDecimal actualCost = BigDecimal.ZERO;
		try {
			actualCost = new BigDecimal(queryAdSettlementInfoCall.getData()
					.getString("cost"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Map> maps = new DefaultWapper()
				.executeSql(String
						.format("SELECT SUM(ISNULL(TotalAmount,0)) AS value FROM dbo.Ymt_SettlementInvoice"
								+ " WHERE SettlementStatus = 1 AND BusinessType = 13"
								+ " AND SellerId = %d AND SettlementTime BETWEEN '%s\' AND '%s'",
								queryAdSettlementInfoReq.getSellerId(),
								queryAdSettlementInfoReq.getStartDate(),
								queryAdSettlementInfoReq.getEndDate()));

		Map map = maps.get(0);

		Logger.verifyEquals((BigDecimal) map.get("value"), actualCost, "校验cost");

	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAdSettlementCost_001() {
		Logger.start(true, "根据时间范围查询广告结算费用信息");
		try {
			String date1 = "2017-11-01T17:19:36";
			String date2 = YMTDateUtil.getBeforeOrNextDay(0,
					"yyyy-MM-dd'T'HH:mm:ss");

			long sellerId = Long.valueOf(EnvSetup.getData("BuyerId"));

			queryAdSettlementInfoReq.setSellerId(sellerId);

			queryAdSettlementInfoReq.setStartDate(date1);

			queryAdSettlementInfoReq.setEndDate(date2);

			queryAdSettlementInfoCall.setData(queryAdSettlementInfoReq);

			queryAdSettlementInfoCall.callService();

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAdSettlementCost_002() {
		Logger.start(true, "sellerId为null");
		try {
			String date1 = YMTDateUtil.getBeforeOrNextDay(-2,
					"yyyy-MM-dd'T'HH:mm:ss");
			String date2 = YMTDateUtil.getBeforeOrNextDay(0,
					"yyyy-MM-dd'T'HH:mm:ss");

			//long sellerId = Long.valueOf(EnvSetup.getData("BuyerId"));

			//queryAdSettlementInfoReq.setSellerId(sellerId);

			queryAdSettlementInfoReq.setStartDate(date1);

			queryAdSettlementInfoReq.setEndDate(date2);

			queryAdSettlementInfoCall.setData(queryAdSettlementInfoReq);

			queryAdSettlementInfoCall.callService();

			//verifyDb();

			Logger.verifyEquals("400",
					queryAdSettlementInfoCall.getString("code"), "校验code");

			Logger.verifyEquals("sellerId:SellerId不能为0",
					queryAdSettlementInfoCall.getString("msg"), "校验msg");

			Logger.verifyEquals("100",
					queryAdSettlementInfoCall.getString("bcode"), "校验bcode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAdSettlementCost_003() {
		Logger.start(true, "StartDate为null");
		try {
			/*String date1 = YMTDateUtil.getBeforeOrNextDay(-2,
					"yyyy-MM-dd'T'HH:mm:ss");*/
			String date2 = YMTDateUtil.getBeforeOrNextDay(0,
					"yyyy-MM-dd'T'HH:mm:ss");

			long sellerId = Long.valueOf(EnvSetup.getData("BuyerId"));

			queryAdSettlementInfoReq.setSellerId(sellerId);

			//queryAdSettlementInfoReq.setStartDate(date1);

			queryAdSettlementInfoReq.setEndDate(date2);

			queryAdSettlementInfoCall.setData(queryAdSettlementInfoReq);

			queryAdSettlementInfoCall.callService();

			//verifyDb();

			Logger.verifyEquals("400",
					queryAdSettlementInfoCall.getString("code"), "校验code");

			Logger.verifyEquals("startDate:may not be null",
					queryAdSettlementInfoCall.getString("msg"), "校验msg");

			Logger.verifyEquals("100",
					queryAdSettlementInfoCall.getString("bcode"), "校验bcode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryAdSettlementCost_004() {
		Logger.start(true, "EndDate为null");
		try {
			String date1 = YMTDateUtil.getBeforeOrNextDay(-2,
					"yyyy-MM-dd'T'HH:mm:ss");
			/*String date2 = YMTDateUtil.getBeforeOrNextDay(0,
					"yyyy-MM-dd'T'HH:mm:ss");*/

			long sellerId = Long.valueOf(EnvSetup.getData("BuyerId"));

			queryAdSettlementInfoReq.setSellerId(sellerId);

			queryAdSettlementInfoReq.setStartDate(date1);

			//queryAdSettlementInfoReq.setEndDate(date2);

			queryAdSettlementInfoCall.setData(queryAdSettlementInfoReq);

			queryAdSettlementInfoCall.callService();

			//verifyDb();

			Logger.verifyEquals("400",
					queryAdSettlementInfoCall.getString("code"), "校验code");

			Logger.verifyEquals("endDate:may not be null",
					queryAdSettlementInfoCall.getString("msg"), "校验msg");

			Logger.verifyEquals("100",
					queryAdSettlementInfoCall.getString("bcode"), "校验bcode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
